using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    //[RequiresAuthentication]
    //[ExceptionLogging]
    public class ProjectController : ContributionController
    {
        #region Attributes
        private ProjectBLL projectBLL;
        private SubjectBLL subjectBLL;
        private ContributionItemBLL contributionItemBLL;
        #endregion

        #region Constructor

        public ProjectController()
        {
            projectBLL = new ProjectBLL();
            subjectBLL = new SubjectBLL();
            contributionItemBLL = new ContributionItemBLL();
        }

        public ProjectController(ProjectBLL projectBLL)
        {
            this.projectBLL = projectBLL;
        }
        #endregion

        #region Create


        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "ContributionID")] Project project)
        {
            if (!ModelState.IsValid)
                return View(); 

            project.AcademicPerson = new AcademicPersonBLL().RetrieveAcademicPersonByID(Convert.ToInt32(CurrentUser.PersonID.Value));
            projectBLL.Create(project);
            return RedirectToAction("Index");
        }

        #endregion

        #region Edit

        [HttpGet]
        public ActionResult Edit(String id)
        {
            Session["id"] = id;
            var project = projectBLL.RetrieveProject(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));

            ViewData["Subjects"] = subjectBLL.RetrieveSubjects(project.ContributionID);

            ViewData["ContributionItems"] = contributionItemBLL.RetrieveContributionItems(project.ContributionID);

            return View(project);
        }

        [HttpPost]
        public ActionResult Edit([Bind(Exclude = "ContributionID")] Project project)
        {
            if (!ModelState.IsValid)
                return View();

            var id = Convert.ToInt32(URLEncrypt.DecryptFromString(Session["id"].ToString()));
            var originalProject = projectBLL.RetrieveProject(id);

            originalProject.Description = project.Description;
            originalProject.Title = project.Title;
            originalProject.SourceInfo = project.SourceInfo;
            originalProject.RegisterDate = project.RegisterDate;

            projectBLL.Edit(originalProject);
            return RedirectToAction("Index");
        }
        #endregion

        #region MyIndex
        [HttpGet]
        public ActionResult MyIndex()
        {
            ViewData["GridSource"] = projectBLL.RetrieveProjects(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }
        [HttpGet]
        public ActionResult Index()
        {
            ViewData["GridSource"] = projectBLL.RetrieveProjects();
            return View();
        }
        #endregion
        [HttpGet]
        public ActionResult Visualize(string id)
        {
            var entity = projectBLL.RetrieveProject(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }
    }

}

